package com.hongan4d.common.VO;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 返回结果统一格式
 *
 * @author tang
 * @version 1.0
 * @date 2021/1/5 15:51
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
    // 错误信息
    private String error;

    private boolean success;

    // 数据封装
    private T result;

    public CommonResult(String error, boolean success) {
        this.error = error;
        this.success = success;
    }

    /**
     * 正确返回结果
     *
     * @param result
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T result) {
        return new CommonResult<>("", true, result);
    }

    public static <T> CommonResult<T> success() {
        return new CommonResult<>("", true);
    }

    public static <T> CommonResult<T> error(String error, T result) {
        return new CommonResult<>(error, false, result);
    }

    public static <T> CommonResult<T> getError(String error) {
        return new CommonResult<T>(error, false);
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }
}
